/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.haoma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import java.math.BigDecimal;
import java.util.Date;
import vip.xiaonuo.biz.modular.haoma.entity.Haoma;
import vip.xiaonuo.biz.modular.haoma.mapper.HaomaMapper;
import vip.xiaonuo.biz.modular.haoma.param.HaomaAddParam;
import vip.xiaonuo.biz.modular.haoma.param.HaomaEditParam;
import vip.xiaonuo.biz.modular.haoma.param.HaomaIdParam;
import vip.xiaonuo.biz.modular.haoma.param.HaomaPageParam;
import vip.xiaonuo.biz.modular.haoma.service.HaomaService;

import vip.xiaonuo.common.util.CommonDownloadUtil;
import vip.xiaonuo.common.util.CommonResponseUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * HAOMAService接口实现类
 *
 * @author admionew
 * @date  2025/07/17 22:23
 **/
@Service
public class HaomaServiceImpl extends ServiceImpl<HaomaMapper, Haoma> implements HaomaService {

    @Override
    public Page<Haoma> page(HaomaPageParam haomaPageParam) {
        QueryWrapper<Haoma> queryWrapper = new QueryWrapper<Haoma>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(haomaPageParam.getHaoma())) {
            queryWrapper.lambda().like(Haoma::getHaoma, haomaPageParam.getHaoma());
        }
        if(ObjectUtil.isNotEmpty(haomaPageParam.getPhaoma())) {
            queryWrapper.lambda().like(Haoma::getPhaoma, haomaPageParam.getPhaoma());
        }
        if(ObjectUtil.isAllNotEmpty(haomaPageParam.getSortField(), haomaPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(haomaPageParam.getSortOrder());
            queryWrapper.orderBy(true, haomaPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(haomaPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(Haoma::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(HaomaAddParam haomaAddParam) {
        Haoma haoma = BeanUtil.toBean(haomaAddParam, Haoma.class);
        this.save(haoma);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(HaomaEditParam haomaEditParam) {
        Haoma haoma = this.queryEntity(haomaEditParam.getId());
        BeanUtil.copyProperties(haomaEditParam, haoma);
        this.updateById(haoma);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<HaomaIdParam> haomaIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(haomaIdParamList, HaomaIdParam::getId));
    }

    @Override
    public Haoma detail(HaomaIdParam haomaIdParam) {
        return this.queryEntity(haomaIdParam.getId());
    }

    @Override
    public Haoma queryEntity(int id) {
        Haoma haoma = this.getById(id);
        if(ObjectUtil.isEmpty(haoma)) {
            throw new CommonException("HAOMA不存在，id值为：{}", id);
        }
        return haoma;
    }

}
